//
//  DownLoadCodeViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/12/14.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "DownLoadCodeViewController.h"

@interface DownLoadCodeViewController ()
@property (nonatomic, strong) NSDictionary *style;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *imageFatherView;
@property (nonatomic, strong) UIScrollView *myScrollView;
@end

@implementation DownLoadCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavi];
    [self setDict];
    [self setMainView];
}

- (void)setNavi
{
    self.bgNavView.hidden = YES;
    UIView *navi = [UIView new];
    if (@available(iOS 11.0, *)) {
        navi.frame = CGRectMake(0, 0, SCREENWIDTH, TopBarHeight_xs);
    } else {
        // Fallback on earlier versions
        navi.frame = CGRectMake(0, 0, SCREENWIDTH, TopBarHeight);
    }
    navi.tag = 20;
    navi.backgroundColor = TotalWhite;
    [self.view addSubview:navi];
    
    UIImageView *leftIV = [UIImageView new];
    leftIV.frame = CGRectMake(15, navi.height-15-18, 18, 14);
    leftIV.image = [UIImage imageNamed:@"back"];
    [navi addSubview:leftIV];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(8, navi.height-40, 32, 28);
    leftBtn.backgroundColor = ClearColor;
    [leftBtn addTarget:self action:@selector(leftBtnBackClick:) forControlEvents:UIControlEventTouchUpInside];
    [navi addSubview:leftBtn];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(navi.width-90, 25, 75, 40);
    saveBtn.layer.cornerRadius = 25;
    saveBtn.titleLabel.textAlignment = NSTextAlignmentRight;
   // saveBtn.backgroundColor = goldColor;
    [saveBtn setTitle:@"保存本地" forState:UIControlStateNormal];
    saveBtn.titleLabel.font = FontMediumWithSize(18);
    [saveBtn setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];
    [navi addSubview:saveBtn];
}

- (void)leftBtnBackClick:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setDict
{
    self.style = @{
                   @"normal":@[TotalWhite,FontWithSize(32)],
                   @"select":@[TotalWhite,FontSemiboldSize(32)],
                   };
}

- (void)setMainView
{
    self.myScrollView = [[UIScrollView alloc] init];
    if (@available(iOS 11.0, *)) {
        self.myScrollView.frame = CGRectMake(0, TopBarHeight_xs, SCREENWIDTH, SCREENHEIGHT-TopBarHeight_xs-BottomHeight_xs);
    } else {
        self.myScrollView.frame = CGRectMake(0, TopBarHeight, SCREENWIDTH, SCREENHEIGHT-TopBarHeight);
    }
    self.myScrollView.contentSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT);
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_myScrollView];
    
    //CGFloat heigh = 410*SCREENWIDTH/375;
    self.imageFatherView = [UIView new];
    self.imageFatherView.backgroundColor = TotalWhite;
    self.imageFatherView.frame = CGRectMake(0, 0, SCREENWIDTH, 600);
    [self.myScrollView addSubview:self.imageFatherView];
    
    self.backView = [UIView new];
    self.backView.frame = CGRectMake(0, 0, SCREENWIDTH, 470);
    self.backView.backgroundColor = UIColorFromHex(0x25AB3A);
    [self.imageFatherView addSubview:self.backView];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(0, 50, SCREENWIDTH, 48);
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = TotalWhite;
    titleLab.font = [UIFont boldSystemFontOfSize:34];
    titleLab.text = @"推荐使用微信支付";
//    titleLab.attributedText = [[NSString stringWithFormat:@"<normal>%@</normal><select>%@</select>",@"推荐使用",@"微信支付"]attributedStringWithStyleBook:self.style];
    [self.backView addSubview:titleLab];
    
    CGFloat imageW = 210;
    CGFloat padding = (SCREENWIDTH-imageW)/2;
    UIImageView *iv = [UIImageView new];
    iv.backgroundColor = TotalWhite;
    iv.frame = CGRectMake(padding, titleLab.bottom+50, imageW, imageW);
    iv.contentMode = UIViewContentModeScaleAspectFill;
   // [iv sd_setImageWithURL:[NSURL URLWithString:self.imageUrl]placeholderImage:[UIImage imageNamed:@"Placeholder400"]];
    //1. 实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2. 恢复滤镜的默认属性
    [filter setDefaults];
    // 3. 将字符串转换成NSData
    NSString *urlStr = self.imageUrl;
    NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
    // 4. 通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKey:@"inputMessage"];
    // 5. 获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];
    // 6. 将CIImage转换成UIImage，并显示于imageView上 (此时获取到的二维码比较模糊,所以需要用下面的createNonInterpolatedUIImageFormCIImage方法重绘二维码)
    iv.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:210];//重绘二维码,使其显示清晰
    self.imageV = iv;
    [self.backView addSubview:iv];
    
    UILabel *nameLab = [UILabel new];
    nameLab.frame = CGRectMake(0, iv.bottom+30, SCREENWIDTH, 33);
    nameLab.textAlignment = NSTextAlignmentCenter;
    nameLab.textColor = TotalWhite;
    nameLab.font = [UIFont boldSystemFontOfSize:24];
    nameLab.text = self.name;
    [self.backView addSubview:nameLab];
    
    CGFloat wechatPadding = (SCREENWIDTH-210)/2;
    UIImageView *wechatIV = [UIImageView new];
    wechatIV.frame = CGRectMake(wechatPadding, self.backView.bottom+38, 56, 56);
    wechatIV.image = [UIImage imageNamed:@"Shape"];
    [self.imageFatherView addSubview:wechatIV];
    
    UILabel *wechatLab = [UILabel new];
    wechatLab.frame = CGRectMake(wechatIV.right+15, self.backView.bottom+37, 160, 56);
    wechatLab.font = FontMediumWithSize(40);
    wechatLab.textColor = UIColorFromHex(0x333333);
    wechatLab.text = @"微信支付";
    [self.imageFatherView addSubview:wechatLab];
    
//    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    saveBtn.frame = CGRectMake(40, self.imageFatherView.bottom+10, SCREENWIDTH-80, 50);
//    saveBtn.layer.cornerRadius = 25;
//    saveBtn.backgroundColor = goldColor;
//    [saveBtn setTitle:@"保存本地" forState:UIControlStateNormal];
//    saveBtn.titleLabel.font = FontMediumWithSize(18);
//    [saveBtn setTitleColor:UIColorFromHex(0x444444) forState:UIControlStateNormal];
//    [saveBtn addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];
//    [self.myScrollView addSubview:saveBtn];
    self.myScrollView.contentSize = CGSizeMake(SCREENWIDTH, self.imageFatherView.bottom+20);
}

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

#pragma mark -- 将view转为图片
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
- (void)saveImage:(UIButton *)btn
{
    UIImage *newImag = [self makeImageWithView:self.imageFatherView withSize:CGSizeMake(SCREENWIDTH, self.imageFatherView.height)];
    UIImage *imageNew = [TTKUtil imageWithImageSimple:newImag scaledToSize:CGSizeMake(400, 600)];
    [self saveImageToPhotos:imageNew];
}

@end
